<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>点云欧式聚类提取 | 荒岛</title><meta name="author" content="Fi9zero"><meta name="copyright" content="Fi9zero"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="欧式聚类🧩 一、整体流程概述该程序实现了一个典型的 “先去背景，再聚类物体” 的点云处理流程：  读取点云 →   体素滤波降采样 →   循环提取多个平面（如桌面、墙面） →   对剩余点进行欧几里得聚类 →   将每个聚类保存为独立的 PCD 文件   ✅ 应用场景：机器人抓取、场景理解、工业检测中识别多个独立物体。   🔧 二、关键技术模块详解   模块 功能说明    VoxelGr">
<meta property="og:type" content="article">
<meta property="og:title" content="点云欧式聚类提取">
<meta property="og:url" content="https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E7%82%B9%E4%BA%91%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E6%8F%90%E5%8F%96/index.html">
<meta property="og:site_name" content="荒岛">
<meta property="og:description" content="欧式聚类🧩 一、整体流程概述该程序实现了一个典型的 “先去背景，再聚类物体” 的点云处理流程：  读取点云 →   体素滤波降采样 →   循环提取多个平面（如桌面、墙面） →   对剩余点进行欧几里得聚类 →   将每个聚类保存为独立的 PCD 文件   ✅ 应用场景：机器人抓取、场景理解、工业检测中识别多个独立物体。   🔧 二、关键技术模块详解   模块 功能说明    VoxelGr">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ckyfi9zero.github.io/image/avatar.jpg">
<meta property="article:published_time" content="2025-08-05T01:56:59.000Z">
<meta property="article:modified_time" content="2025-08-05T05:35:24.207Z">
<meta property="article:author" content="Fi9zero">
<meta property="article:tag" content="c++">
<meta property="article:tag" content="PCL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ckyfi9zero.github.io/image/avatar.jpg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "点云欧式聚类提取",
  "url": "https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E7%82%B9%E4%BA%91%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E6%8F%90%E5%8F%96/",
  "image": "https://ckyfi9zero.github.io/image/avatar.jpg",
  "datePublished": "2025-08-05T01:56:59.000Z",
  "dateModified": "2025-08-05T05:35:24.207Z",
  "author": [
    {
      "@type": "Person",
      "name": "Fi9zero",
      "url": "https://ckyfi9zero.github.io"
    }
  ]
}</script><link rel="shortcut icon" href="/image/favicon.ico"><link rel="canonical" href="https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E7%82%B9%E4%BA%91%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E6%8F%90%E5%8F%96/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"未找到符合您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":true},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '点云欧式聚类提取',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/css/custom.css"><link rel="stylesheet" href="/css/pig.css"><link rel="stylesheet" href="https://npm.elemecdn.com/ethan4116-blog/lib/css/plane_v2.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/image/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/booklist/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(/image/per%20page.jpg);"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">荒岛</span></a><a class="nav-page-title" href="/"><span class="site-name">点云欧式聚类提取</span><span class="site-name"><i class="fa-solid fa-circle-arrow-left"></i><span>  返回首页</span></span></a></span><div id="menus"><div id="search-button"><span class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></span></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/booklist/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">点云欧式聚类提取</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2025-08-05T01:56:59.000Z" title="发表于 2025-08-05 09:56:59">2025-08-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-08-05T05:35:24.207Z" title="更新于 2025-08-05 13:35:24">2025-08-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/PCL/">PCL</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><hr>
<h1 id="欧式聚类"><a href="#欧式聚类" class="headerlink" title="欧式聚类"></a>欧式聚类</h1><h2 id="🧩-一、整体流程概述"><a href="#🧩-一、整体流程概述" class="headerlink" title="🧩 一、整体流程概述"></a>🧩 一、整体流程概述</h2><p>该程序实现了一个典型的 <strong>“先去背景，再聚类物体”</strong> 的点云处理流程：</p>
<ol>
<li><strong>读取点云</strong> →  </li>
<li><strong>体素滤波降采样</strong> →  </li>
<li><strong>循环提取多个平面（如桌面、墙面）</strong> →  </li>
<li><strong>对剩余点进行欧几里得聚类</strong> →  </li>
<li><strong>将每个聚类保存为独立的 PCD 文件</strong></li>
</ol>
<blockquote>
<p>✅ 应用场景：机器人抓取、场景理解、工业检测中识别多个独立物体。</p>
</blockquote>
<hr>
<h2 id="🔧-二、关键技术模块详解"><a href="#🔧-二、关键技术模块详解" class="headerlink" title="🔧 二、关键技术模块详解"></a>🔧 二、关键技术模块详解</h2><table>
<thead>
<tr>
<th>模块</th>
<th>功能说明</th>
</tr>
</thead>
<tbody><tr>
<td><code>VoxelGrid</code></td>
<td>下采样点云，减少计算量，同时保留几何结构；设置 <code>leaf_size=1cm</code> 表示每 1cm³ 内只保留一个代表点</td>
</tr>
<tr>
<td><code>SACSegmentation</code> + <code>SACMODEL_PLANE</code></td>
<td>使用 RANSAC 拟合平面模型，适用于桌面、地板等大平面提取</td>
</tr>
<tr>
<td>循环平面提取</td>
<td>不止提取一个平面，而是反复移除最大平面，直到剩余点云小于原始的 30%，可用于多平面场景（如斜面、台阶）</td>
</tr>
<tr>
<td><code>ExtractIndices</code></td>
<td>核心工具，用于根据索引提取或剔除点，实现“分割-去除-继续”逻辑</td>
</tr>
<tr>
<td><code>KdTree</code></td>
<td>构建空间搜索结构，加速后续聚类中的邻域查询</td>
</tr>
<tr>
<td><code>EuclideanClusterExtraction</code></td>
<td>基于欧氏距离的聚类算法，将空间接近的点归为一类，常用于物体分割</td>
</tr>
</tbody></table>
<hr>
<h2 id="⚙️-三、关键参数解析"><a href="#⚙️-三、关键参数解析" class="headerlink" title="⚙️ 三、关键参数解析"></a>⚙️ 三、关键参数解析</h2><table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
<th>建议值&#x2F;说明</th>
</tr>
</thead>
<tbody><tr>
<td><code>setLeafSize(0.01, 0.01, 0.01)</code></td>
<td>体素大小</td>
<td>通常设为点云分辨率的 1~2 倍；太小无意义，太大丢失细节</td>
</tr>
<tr>
<td><code>setDistanceThreshold(0.02)</code></td>
<td>平面拟合距离阈值</td>
<td>2cm 内认为是平面点；太大会包含非平面点</td>
</tr>
<tr>
<td><code>setMaxIterations(100)</code></td>
<td>RANSAC 最大迭代次数</td>
<td>平面较易拟合，可较低；复杂模型需更高</td>
</tr>
<tr>
<td><code>while (size &gt; 0.3 * nr_points)</code></td>
<td>循环终止条件</td>
<td>防止无限提取；可根据场景调整比例</td>
</tr>
<tr>
<td><code>setClusterTolerance(0.02)</code></td>
<td>聚类距离容差</td>
<td>2cm 内的点视为同一物体；太小会拆分物体，太大会合并不同物体</td>
</tr>
<tr>
<td><code>setMinClusterSize(100)</code></td>
<td>最小聚类点数</td>
<td>过滤噪声或小碎片</td>
</tr>
<tr>
<td><code>setMaxClusterSize(25000)</code></td>
<td>最大聚类点数</td>
<td>防止把整个场景误认为一个物体</td>
</tr>
</tbody></table>
<hr>
<h2 id="💾-四、输出结果"><a href="#💾-四、输出结果" class="headerlink" title="💾 四、输出结果"></a>💾 四、输出结果</h2><ul>
<li>程序不会显式保存中间平面（如桌面），但可通过修改加入保存；</li>
<li>所有 <strong>非平面物体</strong> 被分割成多个聚类，分别保存为：<ul>
<li><code>cloud_cluster_0.pcd</code></li>
<li><code>cloud_cluster_1.pcd</code></li>
<li>…</li>
</ul>
</li>
<li>每个文件代表一个潜在物体（如盒子、杯子、机器部件等）</li>
</ul>
<hr>
<h2 id="📌-五、核心思想提炼"><a href="#📌-五、核心思想提炼" class="headerlink" title="📌 五、核心思想提炼"></a>📌 五、核心思想提炼</h2><blockquote>
<p>🔹 <strong>“先全局后局部”策略</strong>：</p>
<ol>
<li>先用几何模型（平面）去除大面积背景；</li>
<li>再用聚类方法识别散落的独立物体。</li>
</ol>
</blockquote>
<blockquote>
<p>🔹 <strong>迭代式平面提取</strong>：</p>
<ul>
<li>不止提取一个平面；</li>
<li>类似“剥洋葱”，逐层去除显著平面结构；</li>
<li>适用于存在多个平面的复杂场景。</li>
</ul>
</blockquote>
<blockquote>
<p>🔹 <strong>聚类前预处理的重要性</strong>：</p>
<ul>
<li>若不先去除平面，物体可能与地面连在一起，导致聚类失败；</li>
<li>降采样可提升效率，避免过密点影响性能。</li>
</ul>
</blockquote>
<hr>
<h2 id="🛠️-六、可扩展方向（进阶建议）"><a href="#🛠️-六、可扩展方向（进阶建议）" class="headerlink" title="🛠️ 六、可扩展方向（进阶建议）"></a>🛠️ 六、可扩展方向（进阶建议）</h2><table>
<thead>
<tr>
<th>改进方向</th>
<th>实现方式</th>
</tr>
</thead>
<tbody><tr>
<td>添加可视化</td>
<td>使用 <code>pcl::visualization::PCLVisualizer</code> 显示原始点云、平面、聚类结果</td>
</tr>
<tr>
<td>支持颜色信息</td>
<td>改用 <code>pcl::PointXYZRGB</code> 类型，结合颜色聚类</td>
</tr>
<tr>
<td>多种模型拟合</td>
<td>在去平面后尝试拟合圆柱、球体等</td>
</tr>
<tr>
<td>自动参数调节</td>
<td>根据点云密度动态设置 <code>cluster_tolerance</code></td>
</tr>
<tr>
<td>聚类后识别</td>
<td>对每个聚类计算特征（体积、主方向、PCA）进行分类</td>
</tr>
</tbody></table>
<hr>
<h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/ModelCoefficients.h&gt;</span>           <span class="comment">// 存储几何模型参数（如平面方程系数）</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/point_types.h&gt;</span>                 <span class="comment">// 定义点类型，如 pcl::PointXYZ</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/io/pcd_io.h&gt;</span>                   <span class="comment">// 读写 .pcd 文件</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/filters/extract_indices.h&gt;</span>     <span class="comment">// 提取或删除指定索引的点</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/filters/voxel_grid.h&gt;</span>          <span class="comment">// 体素网格滤波，用于下采样降噪</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/features/normal_3d.h&gt;</span>          <span class="comment">// 法线估计（本程序未使用，但头文件存在）</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/kdtree/kdtree.h&gt;</span>               <span class="comment">// KdTree 结构，用于加速最近邻搜索</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/sample_consensus/method_types.h&gt;</span> <span class="comment">// RANSAC 等采样一致性方法</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/sample_consensus/model_types.h&gt;</span>  <span class="comment">// 模型类型：平面、圆柱等</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/segmentation/sac_segmentation.h&gt;</span> <span class="comment">// RANSAC 分割器</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/segmentation/extract_clusters.h&gt;</span> <span class="comment">// 欧几里得聚类分割</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> </span></span><br><span class="line"><span class="function"><span class="title">main</span> <span class="params">(<span class="type">int</span> argc, <span class="type">char</span>** argv)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="comment">// 读取输入点云数据</span></span><br><span class="line">  pcl::PCDReader reader;                                      <span class="comment">// 创建 PCD 读取器对象</span></span><br><span class="line">  pcl::PointCloud&lt;pcl::PointXYZ&gt;::<span class="function">Ptr <span class="title">cloud</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;pcl::PointXYZ&gt;)</span>, <span class="comment">// 原始点云指针</span></span></span><br><span class="line"><span class="function">                                     <span class="title">cloud_f</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;pcl::PointXYZ&gt;)</span></span>; <span class="comment">// 临时存储剩余点云</span></span><br><span class="line">  reader.<span class="built_in">read</span> (<span class="string">&quot;../table_scene_lms400.pcd&quot;</span>, *cloud);         <span class="comment">// 从文件读取点云</span></span><br><span class="line">  std::cout &lt;&lt; <span class="string">&quot;PointCloud before filtering has: &quot;</span> &lt;&lt; cloud-&gt;points.<span class="built_in">size</span> () &lt;&lt; <span class="string">&quot; data points.&quot;</span> &lt;&lt; std::endl; <span class="comment">// 输出原始点数量</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 创建滤波器对象：使用体素网格进行下采样（降采样）</span></span><br><span class="line">  pcl::VoxelGrid&lt;pcl::PointXYZ&gt; vg;                          <span class="comment">// 体素滤波器对象</span></span><br><span class="line">  pcl::PointCloud&lt;pcl::PointXYZ&gt;::<span class="function">Ptr <span class="title">cloud_filtered</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;pcl::PointXYZ&gt;)</span></span>; <span class="comment">// 存储滤波后点云</span></span><br><span class="line">  vg.<span class="built_in">setInputCloud</span> (cloud);                                  <span class="comment">// 设置输入为原始点云</span></span><br><span class="line">  vg.<span class="built_in">setLeafSize</span> (<span class="number">0.01f</span>, <span class="number">0.01f</span>, <span class="number">0.01f</span>);                     <span class="comment">// 设置体素大小为 1cm × 1cm × 1cm</span></span><br><span class="line">  vg.<span class="built_in">filter</span> (*cloud_filtered);                               <span class="comment">// 执行滤波，输出到 cloud_filtered</span></span><br><span class="line">  std::cout &lt;&lt; <span class="string">&quot;PointCloud after filtering has: &quot;</span> &lt;&lt; cloud_filtered-&gt;points.<span class="built_in">size</span> () &lt;&lt; <span class="string">&quot; data points.&quot;</span> &lt;&lt; std::endl; <span class="comment">// 输出滤波后点数</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 创建平面分割对象并设置参数</span></span><br><span class="line">  pcl::SACSegmentation&lt;pcl::PointXYZ&gt; seg;                   <span class="comment">// RANSAC 分割对象</span></span><br><span class="line">  pcl::<span class="function">PointIndices::Ptr <span class="title">inliers</span> <span class="params">(<span class="keyword">new</span> pcl::PointIndices)</span></span>;    <span class="comment">// 存储内点索引（属于模型的点）</span></span><br><span class="line">  pcl::<span class="function">ModelCoefficients::Ptr <span class="title">coefficients</span> <span class="params">(<span class="keyword">new</span> pcl::ModelCoefficients)</span></span>; <span class="comment">// 存储模型参数（如 ax+by+cz+d=0）</span></span><br><span class="line">  pcl::PointCloud&lt;pcl::PointXYZ&gt;::<span class="function">Ptr <span class="title">cloud_plane</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;pcl::PointXYZ&gt; ())</span></span>; <span class="comment">// 存储检测到的平面点</span></span><br><span class="line">  pcl::PCDWriter writer;                                     <span class="comment">// 用于写入 PCD 文件</span></span><br><span class="line"></span><br><span class="line">  seg.<span class="built_in">setOptimizeCoefficients</span> (<span class="literal">true</span>);                        <span class="comment">// 启用系数优化（最小二乘精调）</span></span><br><span class="line">  seg.<span class="built_in">setModelType</span> (pcl::SACMODEL_PLANE);                   <span class="comment">// 模型类型：平面</span></span><br><span class="line">  seg.<span class="built_in">setMethodType</span> (pcl::SAC_RANSAC);                      <span class="comment">// 使用 RANSAC 方法</span></span><br><span class="line">  seg.<span class="built_in">setMaxIterations</span> (<span class="number">100</span>);                               <span class="comment">// 最大迭代次数</span></span><br><span class="line">  seg.<span class="built_in">setDistanceThreshold</span> (<span class="number">0.02</span>);                          <span class="comment">// 距离阈值：2cm 内视为平面点</span></span><br><span class="line"></span><br><span class="line">  <span class="type">int</span> i = <span class="number">0</span>, nr_points = (<span class="type">int</span>) cloud_filtered-&gt;points.<span class="built_in">size</span> (); <span class="comment">// 记录初始点云大小</span></span><br><span class="line">  <span class="keyword">while</span> (cloud_filtered-&gt;points.<span class="built_in">size</span> () &gt; <span class="number">0.3</span> * nr_points)   <span class="comment">// 循环直到剩余点少于原始的 30%</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="comment">// 从剩余点云中分割出最大的平面</span></span><br><span class="line">    seg.<span class="built_in">setInputCloud</span> (cloud_filtered);                      <span class="comment">// 设置当前待处理的点云</span></span><br><span class="line">    seg.<span class="built_in">segment</span> (*inliers, *coefficients);                  <span class="comment">// 执行分割，得到内点和模型参数</span></span><br><span class="line">    <span class="keyword">if</span> (inliers-&gt;indices.<span class="built_in">size</span> () == <span class="number">0</span>)                      <span class="comment">// 如果没有找到平面</span></span><br><span class="line">    &#123;</span><br><span class="line">      std::cout &lt;&lt; <span class="string">&quot;Could not estimate a planar model for the given dataset.&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">      <span class="keyword">break</span>;                                                <span class="comment">// 退出循环</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 提取当前检测到的平面点</span></span><br><span class="line">    pcl::ExtractIndices&lt;pcl::PointXYZ&gt; extract;             <span class="comment">// 提取索引工具</span></span><br><span class="line">    extract.<span class="built_in">setInputCloud</span> (cloud_filtered);                 <span class="comment">// 输入为当前点云</span></span><br><span class="line">    extract.<span class="built_in">setIndices</span> (inliers);                           <span class="comment">// 指定要提取的索引（平面内点）</span></span><br><span class="line">    extract.<span class="built_in">setNegative</span> (<span class="literal">false</span>);                            <span class="comment">// 提取内点（即平面部分）</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 将提取的平面保存到磁盘</span></span><br><span class="line">    extract.<span class="built_in">filter</span> (*cloud_plane);                          <span class="comment">// 执行提取</span></span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;PointCloud representing the planar component: &quot;</span> &lt;&lt; cloud_plane-&gt;points.<span class="built_in">size</span> () &lt;&lt; <span class="string">&quot; data points.&quot;</span> &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 移除已提取的平面点，保留其余点用于后续处理</span></span><br><span class="line">    extract.<span class="built_in">setNegative</span> (<span class="literal">true</span>);                             <span class="comment">// 改为提取“非平面”点</span></span><br><span class="line">    extract.<span class="built_in">filter</span> (*cloud_f);                              <span class="comment">// 提取非平面点存入 cloud_f</span></span><br><span class="line">    cloud_filtered = cloud_f;                               <span class="comment">// 更新 cloud_filtered 为剩余点云</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 创建 KdTree 用于加速聚类搜索</span></span><br><span class="line">  pcl::search::KdTree&lt;pcl::PointXYZ&gt;::<span class="function">Ptr <span class="title">tree</span> <span class="params">(<span class="keyword">new</span> pcl::search::KdTree&lt;pcl::PointXYZ&gt;)</span></span>;</span><br><span class="line">  tree-&gt;<span class="built_in">setInputCloud</span> (cloud_filtered);                     <span class="comment">// 将剩余点云构建成 KdTree</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 开始欧几里得聚类</span></span><br><span class="line">  std::vector&lt;pcl::PointIndices&gt; cluster_indices;           <span class="comment">// 存储多个聚类的索引集合</span></span><br><span class="line">  pcl::EuclideanClusterExtraction&lt;pcl::PointXYZ&gt; ec;        <span class="comment">// 欧几里得聚类对象</span></span><br><span class="line">  ec.<span class="built_in">setClusterTolerance</span> (<span class="number">0.02</span>);                            <span class="comment">// 聚类容差：2cm（同一簇内点间距）</span></span><br><span class="line">  ec.<span class="built_in">setMinClusterSize</span> (<span class="number">100</span>);                               <span class="comment">// 最小簇点数（防止噪声形成小簇）</span></span><br><span class="line">  ec.<span class="built_in">setMaxClusterSize</span> (<span class="number">25000</span>);                             <span class="comment">// 最大簇点数限制</span></span><br><span class="line">  ec.<span class="built_in">setSearchMethod</span> (tree);                                <span class="comment">// 使用 KdTree 加速邻域搜索</span></span><br><span class="line">  ec.<span class="built_in">setInputCloud</span> (cloud_filtered);                        <span class="comment">// 输入是非平面后的剩余点云</span></span><br><span class="line">  ec.<span class="built_in">extract</span> (cluster_indices);                             <span class="comment">// 执行聚类，结果存入 cluster_indices</span></span><br><span class="line"></span><br><span class="line">  <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">for</span> (std::vector&lt;pcl::PointIndices&gt;::const_iterator it = cluster_indices.<span class="built_in">begin</span> (); it != cluster_indices.<span class="built_in">end</span> (); ++it)</span><br><span class="line">  &#123;</span><br><span class="line">    pcl::PointCloud&lt;pcl::PointXYZ&gt;::<span class="function">Ptr <span class="title">cloud_cluster</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;pcl::PointXYZ&gt;)</span></span>; <span class="comment">// 创建新簇点云</span></span><br><span class="line">    <span class="keyword">for</span> (std::vector&lt;<span class="type">int</span>&gt;::const_iterator pit = it-&gt;indices.<span class="built_in">begin</span> (); pit != it-&gt;indices.<span class="built_in">end</span> (); pit++)</span><br><span class="line">      cloud_cluster-&gt;points.<span class="built_in">push_back</span> (cloud_filtered-&gt;points[*pit]); <span class="comment">// 将索引对应点加入簇</span></span><br><span class="line">    cloud_cluster-&gt;width = cloud_cluster-&gt;points.<span class="built_in">size</span> ();   <span class="comment">// 设置宽度（点数）</span></span><br><span class="line">    cloud_cluster-&gt;height = <span class="number">1</span>;                              <span class="comment">// 高度为 1，表示非组织化点云</span></span><br><span class="line">    cloud_cluster-&gt;is_dense = <span class="literal">true</span>;                         <span class="comment">// 假设无 NaN 或无效点</span></span><br><span class="line"></span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;PointCloud representing the Cluster: &quot;</span> &lt;&lt; cloud_cluster-&gt;points.<span class="built_in">size</span> () &lt;&lt; <span class="string">&quot; data points.&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">    std::stringstream ss;</span><br><span class="line">    ss &lt;&lt; <span class="string">&quot;cloud_cluster_&quot;</span> &lt;&lt; j &lt;&lt; <span class="string">&quot;.pcd&quot;</span>;                  <span class="comment">// 文件名：cloud_cluster_0.pcd, ...</span></span><br><span class="line">    writer.<span class="built_in">write</span>&lt;pcl::PointXYZ&gt; (ss.<span class="built_in">str</span> (), *cloud_cluster, <span class="literal">false</span>); <span class="comment">// 写入每个簇为单独 PCD 文件</span></span><br><span class="line">    j++;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> (<span class="number">0</span>); <span class="comment">// 程序正常结束</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://ckyfi9zero.github.io/picx-images-hosting/20250805/image.1ovqv2vh8w.webp" alt="image"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ckyfi9zero.github.io">Fi9zero</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E7%82%B9%E4%BA%91%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E6%8F%90%E5%8F%96/">https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E7%82%B9%E4%BA%91%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E6%8F%90%E5%8F%96/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://ckyfi9zero.github.io" target="_blank">荒岛</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/c/">c++</a><a class="post-meta__tags" href="/tags/PCL/">PCL</a></div><div class="post-share"><div class="social-share" data-image="/image/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><a class="pagination-related  no-desc" href="/2025/08/05/2025-08-05-%E5%9F%BA%E4%BA%8E%E5%8C%BA%E5%9F%9F%E7%94%9F%E9%95%BF%E7%9A%84%E5%88%86%E5%89%B2/" title="基于区域生长的分割"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info"><div class="info-1"><div class="info-item-1">上一篇</div><div class="info-item-2">基于区域生长的分割</div></div></div></a><a class="pagination-related  no-desc" href="/2025/08/05/2025-08-05-%E5%9C%86%E6%9F%B1%E4%BD%93%E6%A8%A1%E5%9E%8B%E5%88%86%E5%89%B2/" title="圆柱体模型分割"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-right"><div class="info-1"><div class="info-item-1">下一篇</div><div class="info-item-2">圆柱体模型分割</div></div></div></a></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8kdtree%E5%AE%9E%E7%8E%B0%E5%BF%AB%E9%80%9F%E9%A2%86%E5%9F%9F%E6%90%9C%E7%B4%A2/" title="用kdtree实现快速领域搜索"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用kdtree实现快速领域搜索</div></div></div></a><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8%E5%85%AB%E5%8F%89%E6%A0%91%E8%BF%9B%E8%A1%8C%E6%97%A0%E5%BA%8F%E7%82%B9%E4%BA%91%E6%95%B0%E6%8D%AE%E7%9A%84%E7%A9%BA%E9%97%B4%E5%8F%98%E5%8C%96%E6%A3%80%E6%B5%8B/" title="用八叉树进行无序点云数据的空间变化检测"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用八叉树进行无序点云数据的空间变化检测</div></div></div></a><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8%E7%9B%B4%E9%80%9A%E6%BB%A4%E6%B3%A2%E5%99%A8%E5%AF%B9%E7%82%B9%E4%BA%91%E8%BF%9B%E8%A1%8C%E6%BB%A4%E6%B3%A2%E5%A4%84%E7%90%86/" title="用直通滤波器对点云进行滤波处理"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用直通滤波器对点云进行滤波处理</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E4%BB%8E%E7%82%B9%E4%BA%91%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%B7%B1%E5%BA%A6%E5%9B%BE%E5%83%8F/" title="从点云创建一个深度图像"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">从点云创建一个深度图像</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E4%BB%8E%E4%B8%80%E4%B8%AA%E7%82%B9%E4%BA%91%E4%B8%AD%E6%8F%90%E5%8F%96%E4%B8%80%E4%B8%AA%E5%AD%90%E9%9B%86/" title="从一个点云中提取一个子集"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">从一个点云中提取一个子集</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E5%9C%A8%E6%BB%A4%E6%B3%A2%E5%99%A8%E6%A8%A1%E5%9D%97%E7%A7%BB%E9%99%A4%E7%A6%BB%E7%BE%A4%E7%82%B9/" title="在滤波器模块移除离群点"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">在滤波器模块移除离群点</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/image/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">Fi9zero</div><div class="author-info-description"></div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/ckyFi9zero"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/ckyFi9zero" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2539894206@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB"><span class="toc-number">1.</span> <span class="toc-text">欧式聚类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%A7%A9-%E4%B8%80%E3%80%81%E6%95%B4%E4%BD%93%E6%B5%81%E7%A8%8B%E6%A6%82%E8%BF%B0"><span class="toc-number">1.1.</span> <span class="toc-text">🧩 一、整体流程概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%94%A7-%E4%BA%8C%E3%80%81%E5%85%B3%E9%94%AE%E6%8A%80%E6%9C%AF%E6%A8%A1%E5%9D%97%E8%AF%A6%E8%A7%A3"><span class="toc-number">1.2.</span> <span class="toc-text">🔧 二、关键技术模块详解</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E2%9A%99%EF%B8%8F-%E4%B8%89%E3%80%81%E5%85%B3%E9%94%AE%E5%8F%82%E6%95%B0%E8%A7%A3%E6%9E%90"><span class="toc-number">1.3.</span> <span class="toc-text">⚙️ 三、关键参数解析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%92%BE-%E5%9B%9B%E3%80%81%E8%BE%93%E5%87%BA%E7%BB%93%E6%9E%9C"><span class="toc-number">1.4.</span> <span class="toc-text">💾 四、输出结果</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%93%8C-%E4%BA%94%E3%80%81%E6%A0%B8%E5%BF%83%E6%80%9D%E6%83%B3%E6%8F%90%E7%82%BC"><span class="toc-number">1.5.</span> <span class="toc-text">📌 五、核心思想提炼</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%9B%A0%EF%B8%8F-%E5%85%AD%E3%80%81%E5%8F%AF%E6%89%A9%E5%B1%95%E6%96%B9%E5%90%91%EF%BC%88%E8%BF%9B%E9%98%B6%E5%BB%BA%E8%AE%AE%EF%BC%89"><span class="toc-number">1.6.</span> <span class="toc-text">🛠️ 六、可扩展方向（进阶建议）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.7.</span> <span class="toc-text">代码实现</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/10/05/2025-10-05-lio_sam/" title="lio_sam">lio_sam</a><time datetime="2025-10-05T02:43:51.000Z" title="发表于 2025-10-05 10:43:51">2025-10-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/12/2025-08-12-%E4%BD%BF%E7%94%A8docker%E6%90%AD%E5%BB%BApytorch%E7%8E%AF%E5%A2%83/" title="使用docker搭建pytorch环境">使用docker搭建pytorch环境</a><time datetime="2025-08-12T12:43:33.000Z" title="发表于 2025-08-12 20:43:33">2025-08-12</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-docker/" title="docker">docker</a><time datetime="2025-08-05T11:02:36.000Z" title="发表于 2025-08-05 19:02:36">2025-08-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-%E8%BF%90%E5%8A%A8%E5%AF%B9%E8%B1%A1%E5%88%86%E5%89%B2%E4%B8%8E%E9%85%8D%E5%87%86%E7%AE%97%E6%B3%95/" title="运动对象分割与配准算法">运动对象分割与配准算法</a><time datetime="2025-08-05T08:16:02.000Z" title="发表于 2025-08-05 16:16:02">2025-08-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E5%88%86%E5%89%B2/" title="条件欧式聚类分割">条件欧式聚类分割</a><time datetime="2025-08-05T07:56:39.000Z" title="发表于 2025-08-05 15:56:39">2025-08-05</time></div></div></div></div></div></div></main><footer id="footer"><div class="footer-other"><div class="footer-copyright"><span class="copyright">&copy;&nbsp;2025 By Fi9zero</span><span class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo 7.3.0</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly 5.4.2</a></span></div><div class="footer_custom_text">Only light can attract bugs.</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"><script>(() => {
  const loadMathjax = () => {
    if (!window.MathJax) {
      window.MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']],
          tags: 'none',
        },
        chtml: {
          scale: 1.1
        },
        options: {
          enableMenu: true,
          renderActions: {
            findScript: [10, doc => {
              for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
                const display = !!node.type.match(/; *mode=display/)
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
                const text = document.createTextNode('')
                node.parentNode.replaceChild(text, node)
                math.start = {node: text, delim: '', n: 0}
                math.end = {node: text, delim: '', n: 0}
                doc.math.push(math)
              }
            }, '']
          }
        }
      }

      const script = document.createElement('script')
      script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
      script.id = 'MathJax-script'
      script.async = true
      document.head.appendChild(script)
    } else {
      MathJax.startup.document.state(0)
      MathJax.texReset()
      MathJax.typesetPromise()
    }
  }

  btf.addGlobalFn('encrypt', loadMathjax, 'mathjax')
  window.pjax ? loadMathjax() : window.addEventListener('load', loadMathjax)
})()</script><script>(() => {
  const runMermaid = ele => {
    window.loadMermaid = true
    const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

    ele.forEach((item, index) => {
      const mermaidSrc = item.firstElementChild
      const mermaidThemeConfig = `%%{init:{ 'theme':'${theme}'}}%%\n`
      const mermaidID = `mermaid-${index}`
      const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent

      const renderFn = mermaid.render(mermaidID, mermaidDefinition)
      const renderMermaid = svg => {
        mermaidSrc.insertAdjacentHTML('afterend', svg)
      }

      // mermaid v9 and v10 compatibility
      typeof renderFn === 'string' ? renderMermaid(renderFn) : renderFn.then(({ svg }) => renderMermaid(svg))
    })
  }

  const codeToMermaid = () => {
    const codeMermaidEle = document.querySelectorAll('pre > code.mermaid')
    if (codeMermaidEle.length === 0) return

    codeMermaidEle.forEach(ele => {
      const preEle = document.createElement('pre')
      preEle.className = 'mermaid-src'
      preEle.hidden = true
      preEle.textContent = ele.textContent
      const newEle = document.createElement('div')
      newEle.className = 'mermaid-wrap'
      newEle.appendChild(preEle)
      ele.parentNode.replaceWith(newEle)
    })
  }

  const loadMermaid = () => {
    if (true) codeToMermaid()
    const $mermaid = document.querySelectorAll('#article-container .mermaid-wrap')
    if ($mermaid.length === 0) return

    const runMermaidFn = () => runMermaid($mermaid)
    btf.addGlobalFn('themeChange', runMermaidFn, 'mermaid')
    window.loadMermaid ? runMermaidFn() : btf.getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaidFn)
  }

  btf.addGlobalFn('encrypt', loadMermaid, 'mermaid')
  window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
})()</script></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/pig.js"></script><script src="/js/weather.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="text-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="站内搜索..." type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>